﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace WizzardTheGame
{
    class Menu
    {
        private Object2D backdrop;
        private Object2D marker;
        private List<MenuItem> options;
        private MenuItem currentOption;
        private bool isActive;
        
        #region Properties

        public Object2D Backdrop { 
            get{ return backdrop;} 
            set{ backdrop = value;} }

        public Object2D Marker { 
            get{return marker;} 
            set{marker = value;} }

        public List<MenuItem> Options { 
            get{return options;} 
            set{options = value;} }

        public MenuItem CurrentOption { 
            get{return currentOption;} 
            set{currentOption = value;} }

        public bool IsActive { 
            get{return isActive;} 
            set{isActive = value;} }

	#endregion

        public Menu()
        {
            Backdrop = new Object2D();
            Backdrop.LoadContent("Sprites\\Background2");
            Backdrop.Rectangle = new Rectangle(0, 0, GameWorld.getInstance().Graphics.Viewport.Width, GameWorld.getInstance().Graphics.Viewport.Height);
            Backdrop.IsVisible = true;

            Options = new List<MenuItem>();

            Marker = new Object2D();
            Marker.LoadContent("Sprites\\Marker");
            Marker.IsVisible = true;

            IsActive = false;
        }

        public void Update()
        {
            if (IsActive)
            {
                bool xbox = true;
                GameWorld gw = GameWorld.getInstance();
#if !XBOX
                xbox = false;
                if (gw.KeyboardState.IsKeyDown(Keys.F12) && !gw.PrevKeyboardState.IsKeyDown(Keys.F12))
                {
                    gw.hideWeaponMenu();
                    gw.showGameLevel();
                }
                if (gw.KeyboardState.IsKeyDown(Keys.Up) && !(gw.PrevKeyboardState.IsKeyDown(Keys.Up)))
                {
                    Previous();
                }
                if (gw.KeyboardState.IsKeyDown(Keys.Down) && !(gw.PrevKeyboardState.IsKeyDown(Keys.Down)))
                {
                    Next();
                }
                if (gw.KeyboardState.IsKeyDown(Keys.Enter) && !(gw.PrevKeyboardState.IsKeyDown(Keys.Enter)))
                {
                    CurrentOption.CallActivate();
                }
#endif
                if (xbox)
                {
                    if (gw.GamePadState.IsButtonDown(Buttons.Start) && !gw.PrevGamePadState.IsButtonDown(Buttons.Start))
                    {
                        gw.hideWeaponMenu();
                        gw.showGameLevel();
                    }
                    if (gw.GamePadState.IsButtonDown(Buttons.DPadUp) && !(gw.PrevGamePadState.IsButtonDown(Buttons.DPadUp)))
                    {
                        Previous();
                    }
                    if (gw.GamePadState.IsButtonDown(Buttons.DPadDown) && !(gw.PrevGamePadState.IsButtonDown(Buttons.DPadDown)))
                    {
                        Next();
                    }
                    if (gw.GamePadState.IsButtonDown(Buttons.X) && !(gw.PrevGamePadState.IsButtonDown(Buttons.X)))
                    {
                        CurrentOption.CallActivate();
                    }
                }
            }
        }

        public virtual void Draw()
        {
            if (IsActive)
            {
                //Backdrop.Draw();
                foreach (MenuItem option in options)
                {
                    option.Draw();
                }
                Marker.Draw();
                
            }
        }

        public void Next()
        {
            int i = Options.IndexOf(CurrentOption);
            i++;
            if (i < Options.Count)
            {
                CurrentOption = Options[i];
            }
            else
            {
                CurrentOption = Options[0];
            }
            changeMarker();
        }

        public void Previous()
        {
            int i = Options.IndexOf(CurrentOption);
            i--;
            if (i >= 0)
            {
                CurrentOption = Options[i];
            }
            else
            {
                CurrentOption = Options[(Options.Count-1)];
            }
            changeMarker();
        }

        private void changeMarker()
        {
            Marker.Rectangle = new Rectangle(0, (int)CurrentOption.Text.Position.Y-(Marker.Sprite.Height/2), Marker.Sprite.Width, Marker.Sprite.Height);
        }
    
    }
}
